﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    /// <summary>
    /// 受理单信息列表
    /// </summary>
    /// create by liuyunfei at 2017/03/27
    public class Zor_Acceptance_Bll<T> where T : class
    {
        /// <summary>
        /// 获取受理单信息列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount, string where)
        {
            var uid = RequestSession.GetSessionUser().User_Id;
            var dtid = RequestSession.GetSessionUser().DT_Id;
            var sqlSurch = string.Empty;
            if (Model.Lease_StartTime == null && 
                Model.Lease_EndTime == null &&
                Model.Acceptance_TicketNumber == null &&
                Model.State == null &&
                Model.Acceptance_TaxNumber == null &&
                Model.Tenant_Name == null
               )
            {
                sqlSurch += "(State = '已保存' or State = '被退回') and ";
            }
            if (Model.Lease_StartTime != null)
            {
                int st = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString()));
                sqlSurch += "Lease_StartTime = " + st + " and ";
            }
            if (Model.Lease_EndTime != null)
            {
                int et = TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString()));
                sqlSurch += "Lease_EndTime = " + et + " and ";
            }
            if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += "Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "' and ";
            }
            if (Model.State != null && Model.State != "全部")
            {
                sqlSurch += "State = '" + Model.State + "' and ";
            }
            if (Model.Acceptance_TaxNumber != null)
            {
                sqlSurch += "Acceptance_TaxNumber = '" + Model.Acceptance_TaxNumber + "' and ";
            }
            if (Model.Tenant_Name != null)
            {
                sqlSurch += "Tenant_Name like '%" + Model.Tenant_Name + "%' and ";
            }
            sqlSurch += " Dt_Id =" + dtid + " ";
            return PagerHelp<T>.GetPagerList(index, "V_Acceptance", "Acceptance_CreateTime desc ", out TotalCount, sqlSurch);
        }

        /// <summary>
        /// 获取受理单打印-社区列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageListSq(int index, out int TotalCount, string where)
        {
            var dtid = RequestSession.GetSessionUser().DT_Id;
            return PagerHelp<T>.GetPagerList(index, "V_Acceptance", "Acceptance_Id", out TotalCount, " Acceptance_State = 1 and Dt_Id =" + dtid + " ");
        }

        /// <summary>
        /// 获取受理单打印-街道办列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageListJdb(int index, out int TotalCount, string where)
        {
            var dtid = RequestSession.GetSessionUser().DT_Id;
            return PagerHelp<T>.GetPagerList(index, "V_Acceptance", "Acceptance_CreateTime desc ", out TotalCount, " Acceptance_State = 5 and Dt_Id in (select Dt_Id from Sys_Department where Dt_ParentId = " + dtid + " )");
        }

        /// <summary>
        /// 受理单管理-修改
        /// </summary>
        /// <param name="acceptance">受理单对象</param>
        /// <returns>应答</returns>
        public static BaseAnswer UpdateAcceptance(int PropertyTax, int PersonalTax, int Acceptance_Tax, int Acceptance_TotalTax, Zor_Acceptance acceptance, DateTime startTime, DateTime endTime)
        {
            BaseAnswer bar = Zor_Acceptance_DAL<T>.UpdateAcceptance(PropertyTax,PersonalTax,Acceptance_Tax,Acceptance_TotalTax,acceptance, startTime, endTime);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败,请检查受理单信息是否重复！";
            return bar;
        }

        /// <summary>
        /// 租赁管理-受理单新增
        /// </summary>
        /// <param name="lease">租赁对象</param>
        /// <returns>执行结果</returns>
        public static BaseAnswer InsertAcceptance(string LeaseId, int PropertyTax, int PersonalTax, int Acceptance_Tax, int Acceptance_TotalTax, string Acceptance_TaxNumber, string Acceptance_Address, DateTime startTime, DateTime endTime)
        {
            BaseAnswer bar = Zor_Acceptance_DAL<T>.InsertAcceptance(LeaseId, PropertyTax, PersonalTax, Acceptance_Tax, Acceptance_TotalTax, Acceptance_TaxNumber, Acceptance_Address, startTime, endTime);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "增加成功！" : "新增失败，请检查租赁信息是否已存在！";
            return bar;
        }

        /// <summary>
        /// 受理单信息删除
        /// </summary>
        /// <param name="id">受理单信息ID集合</param>
        /// <returns></returns>
        public static BaseAnswer DeleteAcceptance(int[] id)
        {
            BaseAnswer bar = Zor_Acceptance_DAL<T>.DeleteAcceptance(id);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "删除成功！" : "删除失败，请重试！";
            return bar;
        }
        /// <summary>
        /// 查询打印区域记录
        /// </summary>
        /// <param name="dayReport_id"></param>
        /// <returns></returns>

        public static List<T> GetAcceptancePrint(int Acceptance_Id)
        {
            return Zor_Acceptance_DAL<T>.GetAcceptancePrint(Acceptance_Id);
        }
        
  
    }
}
